<template>
  <by-background />

  <by-navigation-bar :title="pageInfo.title" />

  <view class="padding-20">
    <view v-if="type === 0 || type === 1" class="page bg-white padding-tb-20 padding-lr-30 margin-bottom-20">
      <input v-model="contactInfo" class="width-full text-28 color-t1" type="number" maxlength="11" placeholder="请输入联系方式" placeholder-class="color-placeholder">
    </view>

    <view class="page bg-white padding-tb-20 padding-lr-30">
      <textarea
        :value="content"
        :placeholder="pageInfo.placeholder"
        :maxlength="100"
        placeholder-style="color: #c2c2c2"
        class="textarea width-full text-28 color-t1"
        @input="setValue"
      />
      <view class="flex-row justify-end margin-top-20">
        <text class="text-26 color-placeholder">{{content.length}}/100</text>
      </view>
    </view>

    <view v-if="type !== 3" class="padding-lr-10 margin-top-80">
      <by-button height="78rpx" font-size="32rpx" bold text="提 交" @click="submit"/>
    </view>
  </view>

  <by-bottom-button v-if="type === 3" height="72rpx" font-size="32rpx" bold text="确 定" @click="submit"/>
</template>

<script setup lang="ts">
import useUserStore from '@/store/user'
import { toast } from '@/common/util'
import { updateInfo, technicianAddReport, userAddReport, feedBack, contactUs } from '@/common/api'
import type { AnyObject } from '@/common/types'

/* 页面初始数据 start*/
const userStore = useUserStore()
interface IPageInfo{
  title?: string
  placeholder?: string
  toast: string
}
const pageInfos:IPageInfo[] = [
  {
    title: '用户反馈',
    placeholder: '请填写10字以上的问题描述或者建议详情...',
    toast: '请填写问题描述或者建议'
  },
  {
    title: '商务合作',
    placeholder: '请填写合作意向...',
    toast: '请填写合作意向'
  },
  {
    title: '举报',
    placeholder: '请填写10字以上的举报内容...',
    toast: '请填写举报内容'
  },
  {
    title: '简介编辑',
    placeholder: '请填写10字以上的个人简介...',
    toast: '请填写个人简介'
  },
  {
    title: '用户投诉',
    placeholder: '请填写10字以上的问题描述或者建议详情...',
    toast: '请填写问题描述或者建议'
  },
]
const type = ref(-1)
const pageInfo = shallowRef<IPageInfo>({
  toast: ''
})
onLoad((e) => {
  const inx = type.value = parseInt(e?.type)
  pageInfo.value = pageInfos[inx]
  id = e?.id
  from = e?.from
  if (e?.type === '3') {
    content.value = userStore.userInfo.pkProperties?.profile ?? userStore.userInfo.profile ?? ''
  }
})
/* 页面初始数据 end*/

/* 修改提交的数据 start*/
const content = ref('')
const contactInfo = ref('')
function setValue(e:AnyObject) {
  let { value } = e.detail
  if (value) value = value.replace(/\s+/g, '')
  content.value = value
}
/* 修改提交的数据 end*/

/* 提交内容 start*/
let id:string
let from:string
async function submit() {
  if ((type.value === 0 || type.value === 1 || type.value === 4) && !contactInfo.value) {
    toast('请输入联系方式')
    return
  }
  if (!content.value) {
    toast(pageInfo.value.toast)
    return
  }
  if (content.value.length < 10) {
    toast('内容过少，在补充一点吧')
    return
  }

  if (type.value === 0 || type.value === 4) {
    await feedBack({
      content: content.value,
      contactInfo: contactInfo.value
    })
    toast('提交成功')
  }

  if (type.value === 1) {
    await contactUs({
      content: content.value,
      contactInfo: contactInfo.value
    })
    toast('提交成功')
  }

  if (from === '0') {
    await userAddReport({
      tno: id,
      content: content.value
    })
    toast('提交成功')
  }

  if (from === '1') {
    await technicianAddReport({
      cno: id,
      content: content.value
    })
    toast('提交成功')
  }

  if (type.value === 3) {
    await updateInfo({
      profile: content.value
    })
    toast('修改成功，请等待审核')
  }
}
/* 提交内容 end*/
</script>

<style scoped lang="scss">
@include setMargin(top, 80);

.page{
  border-radius: 16rpx;

  .textarea{
    height: 300rpx;
  }

}
</style>
